package com.symbio.ishenapi.background.controller;

import com.symbio.ishenapi.background.bean.MedicalAssistance;
import com.symbio.ishenapi.background.model.TmMedicalAssistance;
import com.symbio.ishenapi.background.model.TmMedicalAssistancePic;
import com.symbio.ishenapi.common.bean.LoginUser;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * 联系医助
 */
@RequestMapping("/tmMedicalAssistanceController")
public class TmMedicalAssistanceController extends BaseController {


    /**
     * 根据类型查询全部联系医助
     */
    public void findTmBrandMaterials(){
        int sourceType = getParaToInt("sourceType");
        String submitName = getPara("submitName");
        LoginUser loginUser = getLoginUser();
        String Ids = loginUser.getDoctorIds();
        if (Ids.endsWith(",")) {
            Ids = Ids.substring(0,(Ids.length() - 1));
        }
        Map map = TmMedicalAssistance.dao.findsourceTypeByAssistance(sourceType,submitName,getQueryModel() ,Ids);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }


    /**
     * 根据申请id获取数据
     */
    public void findById(){
        //System.out.println("1111111111111111==========="+getReqBodyString());
        TmMedicalAssistance user = (TmMedicalAssistance)getReqBodyObject(TmMedicalAssistance.class);
        System.out.println("1111111111111111==========="+user.getId());
       if(user.getId() == null || "".equals( user.getId())){
            renderJsonError(40003, "联系医助编号");
        }
        TmMedicalAssistance tmMedicalAssistance = TmMedicalAssistance.dao.findById(user.getId());
        TmMedicalAssistancePic tmMedicalAssistancePic = new TmMedicalAssistancePic();
        List<TmMedicalAssistancePic> list = tmMedicalAssistancePic.findMedicalAssistanceIdByPic(tmMedicalAssistance.getId(),tmMedicalAssistance.getSourceType());
        //tmMedicalAssistance.setMedicalAssistanceList(list);

        MedicalAssistance medicalAssistance = new MedicalAssistance();
        medicalAssistance.setId(tmMedicalAssistance.getId());
        medicalAssistance.setContent(tmMedicalAssistance.getContent());
        medicalAssistance.setCreateTime(tmMedicalAssistance.getCreateTime());
        medicalAssistance.setSourceType(tmMedicalAssistance.getSourceType());
        medicalAssistance.setStatus(tmMedicalAssistance.getStatus());
        medicalAssistance.setSubmitUserId(tmMedicalAssistance.getSubmitUserId());
        medicalAssistance.setSubmitName(tmMedicalAssistance.getSubmitName());
        medicalAssistance.setSubmitTime(tmMedicalAssistance.getSubmitTime());
        medicalAssistance.setMedicalAssistanceList(list);

        renderJsonRet(medicalAssistance);
        //renderJsonRet(40001);
    }

    /**
     * 修改已读状态
     */
    public void updateStatus() {
        TmMedicalAssistance user = (TmMedicalAssistance) getReqBodyObject(TmMedicalAssistance.class);
        TmMedicalAssistance u = TmMedicalAssistance.dao.findById(user.getId());
        if (u != null) {
            u.setStatus(user.getStatus());
            if (u.update()) {
                renderJsonRet(user.getId());
            } else {
                renderJsonError(40005);
            }
        } else {
            renderJsonError(40003);
            return;
        }
    }
}
